﻿using UnityEngine;
using System.Collections.Generic;
using System;

public class GameView : MonoBehaviour {
	public UILabel housePrice;
	public UILabel leftTime;
	public UILabel cash;
	public UILabel health;
	public UILabel deposit;
	public UILabel reputation;

	public UIButton secHandMarket;
	public UIButton farmingMarket;
	public UIButton wholesaleMarket;
	public UIButton bank;
	public UIButton hospital;
	public UIButton houseSale;
	public UIButton intermediary;
	public UIButton help;
	public UIButton start;
	public UIButton stop;
	public UIButton milestone;

	public GameObject marketRect;
	public GameObject storageRect;

	const int MAX_COUNT = 5;
	private CommodityView[] _commodityForSales = new CommodityView[MAX_COUNT];
	private CommodityView[] _commodityOfMine = new CommodityView[MAX_COUNT];

	private PlayerInfo _playerInfo;

	public List<Commodity> commodityForSale {
		set {
			for (int i=0; i<_commodityForSales.Length; i++){
				if (i<value.Count){
					_commodityForSales[i].gameObject.SetActive(true);
					_commodityForSales[i].commodity = value[i];
				}
				else {
					_commodityForSales[i].gameObject.SetActive(false);
				}
			}
		}
	}

	public List<Commodity> commodityOfMine {
		set {
			for (int i=0; i<_commodityOfMine.Length; i++){
				if (i<value.Count){
					_commodityOfMine[i].gameObject.SetActive(true);
					_commodityOfMine[i].commodity = value[i];
				}
				else {
					_commodityOfMine[i].gameObject.SetActive(false);
				}
			}
		}
	}

	void Awake () {
		initComponent();
	}

	// Update is called once per frame
	void Update () {
		/*
		var values = Enum.GetValues(typeof(KeyCode)); 
		for(int x = 0; x < values.Length; x++) { 
			if(Input.GetKeyDown((KeyCode)values.GetValue(x))){ 
				_currentButton = values.GetValue(x).ToString(); 
			} 
		}
		*/

		if (Input.GetKeyUp(KeyCode.Escape)){
			Application.Quit();
		}

		if (_playerInfo != null){
			cash.text = _playerInfo.cash.ToString();
			health.text = _playerInfo.health.ToString();
			deposit.text = _playerInfo.deposit.ToString();
			reputation.text = _playerInfo.reputation.ToString();
		}
	}

	void OnApplicationQuit()
	{

	}

	private void initComponent()
	{
		UIEventListener.Get(secHandMarket.gameObject).onClick += onSecHandMarketClick;
		UIEventListener.Get(farmingMarket.gameObject).onClick += onFarmingMarketClick;
		UIEventListener.Get(wholesaleMarket.gameObject).onClick += onWholesaleMarkettClick;
		UIEventListener.Get(bank.gameObject).onClick += onBankClick;
		UIEventListener.Get(hospital.gameObject).onClick += onHospitalClick;
		UIEventListener.Get(houseSale.gameObject).onClick += onHouseSaleClick;
		UIEventListener.Get(intermediary.gameObject).onClick += onIntermediaryClick;
		UIEventListener.Get(help.gameObject).onClick += onHelpClick;
		UIEventListener.Get(start.gameObject).onClick += onStartClick;
		UIEventListener.Get(stop.gameObject).onClick += onStopClick;
		UIEventListener.Get(milestone.gameObject).onClick += onMilestoneClick;

		Transform marketRectTrans = marketRect.transform;
		Transform storageRectTrans = storageRect.transform;

		for (int i=0; i<MAX_COUNT; i++){
			_commodityForSales[i] = marketRectTrans.FindChild("Comodity" + i).GetComponent<CommodityView>();
			_commodityForSales[i].onCommodityClick += onCommodityForSaleClick;

			_commodityOfMine[i] = storageRectTrans.FindChild("Comodity" + i).GetComponent<CommodityView>();
			_commodityOfMine[i].onCommodityClick += onCommodityOfMineClick;
		}
		enablePlayingState(false);
	}

	private void enablePlayingState(bool isPlaying)
	{
		secHandMarket.enabled = isPlaying;
		farmingMarket.enabled = isPlaying;
		wholesaleMarket.enabled = isPlaying;
		bank.enabled = isPlaying;
		hospital.enabled = isPlaying;
		houseSale.enabled = isPlaying;
		intermediary.enabled = isPlaying;
		help.enabled = isPlaying;
		milestone.enabled = isPlaying;

		start.gameObject.SetActive(!isPlaying);
		stop.gameObject.SetActive(isPlaying);
		marketRect.SetActive(isPlaying);
		storageRect.SetActive(isPlaying);
	}

	void onSecHandMarketClick(GameObject btn)
	{
		commodityForSale = CommodityConfig.Instance.generate ();
	}

	void onFarmingMarketClick(GameObject btn)
	{
		commodityForSale = CommodityConfig.Instance.generate ();
	}
	void onWholesaleMarkettClick(GameObject btn)
	{
		commodityForSale = CommodityConfig.Instance.generate ();
	}
	void onBankClick(GameObject btn)
	{
		
	}
	void onHospitalClick(GameObject btn)
	{
		
	}
	void onHouseSaleClick(GameObject btn)
	{
		
	}
	void onIntermediaryClick(GameObject btn)
	{
		
	}
	void onHelpClick(GameObject btn)
	{
		
	}
	void onStartClick(GameObject btn)
	{
		enablePlayingState(true);
		_playerInfo = new PlayerInfo();
		commodityForSale = CommodityConfig.Instance.generate ();
		commodityOfMine = _playerInfo.commodities;
	}

	void onStopClick(GameObject btn)
	{
		enablePlayingState(false);
	}

	void onMilestoneClick(GameObject btn)
	{
		
	}

	private void onCommodityForSaleClick(Commodity commodity)
	{
		Debug.Log ("Buy commodity:" + commodity);
		int maxBuyCount = _playerInfo.cash / commodity.price;

		if (maxBuyCount > 0) {
			Commodity buyedCommodity = commodity.clone();
			buyedCommodity.num = maxBuyCount;
			_playerInfo.cash -= maxBuyCount * commodity.price;
			_playerInfo.commodities.Add(buyedCommodity);
			commodityOfMine = _playerInfo.commodities;
		}
		else {
			Debug.LogWarning("不够钱！");
		}
	}

	private void onCommodityOfMineClick(Commodity commodity)
	{
		Debug.Log ("Sale commodity:" + commodity);
		Commodity commForSale = findCommodityInSaleList (commodity.name);

		if (commForSale != null) {
			int money = commForSale.price * commodity.num;
			_playerInfo.cash += money;
			_playerInfo.commodities.Remove(commodity);
			commodityOfMine = _playerInfo.commodities;
		}
	}

	private Commodity findCommodityInSaleList(string name)
	{
		for (int i=0; i<_commodityForSales.Length; i++){
			if (name.Equals(_commodityForSales[i].commodity.name)){
				return _commodityForSales[i].commodity;
			}
		}

		return null;
	}
}
